#include "getmessagestatusresponse.h"
#include "appchat/accountmanager.h"
#include "account/accountmember.h"


#include <QDebug>


GetMessageStatusResponse::GetMessageStatusResponse(qint64 fid, QObject *parent)
    : JSONResponse(parent),
    m_fid(fid)
{
}


const QString &GetMessageStatusResponse::messageStatus() const
{
    return m_messageStatus;
}

const qint64 &GetMessageStatusResponse::getfid() const
{
    return m_fid;
}


void GetMessageStatusResponse::parse(const QVariant &data)
{
    if (data.type() != QVariant::Map){
        setError(Network::Response::Property, "The field of \"map\" isn't a map");
        return;
    }

    QVariantMap map = data.toMap();

    if (!map.contains("list")){
        setError(Response::Property, "The field of \"list\" is missing");
        return;
    }

    QVariant listVariant = map.value("list");
    if (listVariant.type() != QVariant::List){
        setError(Response::Property, "The value of the field of \"list\" is incompatible");
        return;
    }

    QVariantList list = listVariant.toList();

    foreach (QVariant item, list) {
        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The field of \"map\" isn't a map"));
            return;
        }

        QVariantMap itemMap = item.toMap();
        if (!itemMap.contains("uid")) {
            setError(Response::Property, "The field of \"uid\" is missing");
            return;
        }
        // QVariant idVariant = itemMap.value("uid");

        if (!itemMap.contains("status")) {
            setError(Response::Property, "The field of \"status\" is missing");
            return;
        }

        QVariant statusVariant = itemMap.value("status");
        if (statusVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"status\" is incompatible");
            return;
        }

        m_messageStatus = statusVariant.toString();

        return;
    }
}
